Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Data.Odbc
Public Class FrmMain

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        InsertAudit_LogOut()
        End
    End Sub
    Private Sub InsertAudit_LogOut()
        Try
            Dim myCommand As SqlCommand
            Dim vSQL As String
            'Response.Write(e.Item.Cells(4).Text)
            'CONNETION STRING
            Dim SqlConn As New SqlConnection()
            SqlConn = New SqlConnection(GV_CONNETION)
            myCommand = New SqlCommand("spInsertAuditLog", SqlConn)
            myCommand.CommandType = CommandType.StoredProcedure
            SqlConn.Open()

            myCommand.Parameters.Add("@UserId", SqlDbType.VarChar)
            myCommand.Parameters.Add("@STATUS", SqlDbType.VarChar)
            myCommand.Parameters("@UserId").Value = GV_USER
            myCommand.Parameters("@STATUS").Value = "OUT"
            myCommand.ExecuteNonQuery()
            myCommand.Cancel()
            SqlConn.Close()
            myCommand = Nothing
            SqlConn = Nothing

        Catch ex As Exception
            Dim vStrErr As String
            vStrErr = ex.Message & " ||InsertAuditLog||"
            Write_ErrLog(vStrErr)
            Exit Sub
        End Try

    End Sub

    Private Sub Write_ErrLog(ByVal vStrErr)
        'Write error log into errlog.txt
        Dim strAppPath
        strAppPath = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\") - 1)

        Dim myStreamWriter As New System.IO.StreamWriter(strAppPath & "\Errlog.txt", True)
        myStreamWriter.WriteLine(Now & "  " & vStrErr)
        myStreamWriter.Flush()
        myStreamWriter.Close()
    End Sub
    Private Sub PatientBiodataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PatientBiodataToolStripMenuItem.Click
        ' Dim oFrmBiodata As New FrmBiodata()
        ' oFrmBiodata.Show()
        FrmBiodata.Close()
        FrmBiodata.Show()
    End Sub

    Private Sub FrmMain_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If GV_CUR_PATNRIC = "" Then
            Button1.Text = "Select Patient"
            Button1.Refresh()
        Else
            Button1.Text = GV_CUR_PATNRIC + " " + GV_CUR_PATNAME
            Button1.Refresh()
        End If
    End Sub

    Private Sub FrmMain_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        InsertAudit_LogOut()
        End
    End Sub

    Private Sub FrmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Text = "Breast Tumor Board " & "  Login User: " & GV_USERNAME
        LoadUserSecurity_Biodata()

        PatientBiodataToolStripMenuItem.Enabled = GV_FRMBIODATA_ALLOW_VIEW
        DiaToolStripMenuItem.Enabled = GV_FRMEVENT_ALLOW_VIEW
        SecurityControlToolStripMenuItem1.Visible = GV_FRMSECURITY_VIEW
        AddNewUserToolStripMenuItem1.Visible = GV_FRMCREATE_NEWUSER_VIEW
        'btnLog.BackColor = Color.Green
        'btnLog.ImageIndex = 0
        'btnLog.Text = "In"
        'btnLog.Refresh()

        'Import button only  visible for Kalai and John
        If (GV_USER = 199 Or GV_USER = 207) Then
            Me.ImportButton.Visible = True
        Else
            Me.ImportButton.Visible = False
        End If

    End Sub
    Private Sub LoadUserSecurity_Event()
        Dim vStr As String

        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        vStr = "select * from Security_Form where User_Name ='" & GV_USER & "'  and Form_NM = 'EVENT'"
        Dim sqlCmd = New SqlCommand(vStr, SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            GV_FRMEVENT_ALLOW_VIEW = False
            GV_FRMEVENT_ALLOW_EDIT = False
            GV_FRMEVENT_ALLOW_APPROVE = False
            Do While objReader.Read
                GV_FRMEVENT_ALLOW_VIEW = IIf(IsDBNull(objReader("Sec_View")), False, objReader("Sec_View"))
                GV_FRMEVENT_ALLOW_EDIT = IIf(IsDBNull(objReader("Sec_Edit")), False, objReader("Sec_Edit"))
                GV_FRMEVENT_ALLOW_APPROVE = IIf(IsDBNull(objReader("Sec_Approve")), False, objReader("Sec_Approve"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Public Sub LoadUserSecurity_Biodata()
        Dim vStr As String

        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        vStr = "select * from UserPermissions  where UserId =  '" & GV_USER & "'"
        Dim sqlCmd = New SqlCommand(vStr, SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            GV_FRMBIODATA_ALLOW_ADD = False
            GV_FRMBIODATA_ALLOW_VIEW = False
            GV_FRMBIODATA_ALLOW_EDIT = False
            GV_FRMBIODATA_ALLOW_APPROVE = False
            GV_FRMBIODATA_ALLOW_DELETE = False
            GV_FRMBIODATA_ALLOW_UNAPPROVE = False
            GV_FRMEVENT_ALLOW_VIEW = False
            GV_FRMEVENT_ALLOW_EDIT = False
            GV_FRMEVENT_ALLOW_APPROVE = False
            GV_FRMDELETE_ENTIRE_PATDATA = False
            GV_FRMSECURITY_VIEW = False
            GV_FRMCREATE_NEWUSER_VIEW = False

            Do While objReader.Read
                If objReader("PermissionId") = 18 Then GV_FRMBIODATA_ALLOW_VIEW = True
                If objReader("PermissionId") = 17 Then GV_FRMBIODATA_ALLOW_EDIT = True
                If objReader("PermissionId") = 6 Then GV_FRMBIODATA_ALLOW_DELETE = True
                If objReader("PermissionId") = 20 Then GV_FRMBIODATA_ALLOW_UNAPPROVE = True
                If objReader("PermissionId") = 19 Then GV_FRMBIODATA_ALLOW_APPROVE = True
                If objReader("PermissionId") = 5 Then GV_FRMBIODATA_ALLOW_ADD = True
                If objReader("PermissionId") = 6 Then GV_FRMDELETE_ENTIRE_PATDATA = True
                If objReader("PermissionId") = 9 Then GV_FRMEVENT_ALLOW_VIEW = True
                If objReader("PermissionId") = 10 Then GV_FRMEVENT_ALLOW_EDIT = True
                If objReader("PermissionId") = 11 Then GV_FRMEVENT_ALLOW_APPROVE = True
                If objReader("PermissionId") = 14 Then GV_FRMSECURITY_VIEW = True
                If objReader("PermissionId") = 15 Then GV_FRMCREATE_NEWUSER_VIEW = True
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim oFrmSearchPat As New FrmSearchPat()
        'FrmSearchPat.Show()
        FrmDiagnosis.Close()
        FrmSearchPat.Show()
        FrmSearchPat.Focus()

    End Sub

    Private Sub DiaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DiaToolStripMenuItem.Click
        If GV_CUR_PATID = "" Then
            MsgBox("Please select patient")
            FrmSearchPat.Show()
            FrmSearchPat.Focus()
            Exit Sub
        End If
        'FrmDiagnosis.MdiParent = Me
        FrmDiagnosis.Show()
        FrmDiagnosis.Focus()
    End Sub


    Private Sub PresentationToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PresentationToolStripMenuItem.Click

        'If GV_CUR_PATID = "" Then
        'MsgBox("Please select patient")
        'FrmSearchPat.Show()
        'FrmSearchPat.Focus()
        'Exit Sub
        'End If
        'FrmDiagnosis.MdiParent = Me
        FrmPresentation.Show()
        FrmPresentation.Focus()
    End Sub

    Private Sub ImportToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles ImportToolStripMenuItem1.Click
        FrmImport.Show()
        FrmImport.Focus()
    End Sub

    Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

    End Sub

    Private Sub btnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLog.Click
        '   btnLog.BackColor = Color.Red
        'btnLog.ImageIndex = 1
        ' btnLog.Text = "Out"
        btnLog.Refresh()
        InsertAudit_LogOut()
        'FrmLogout.ShowDialo()
        FrmLogin.Show()
        Me.Hide()

        GV_CUR_PATID = ""
        GV_CUR_PATNRIC = ""
        GV_CUR_PATNAME = ""
    End Sub

    'Private Sub LogoutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogoutToolStripMenuItem.Click
    '   btnLog_Click(sender, e)
    'End Sub

    Private Sub SecurityControlToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SecurityControlToolStripMenuItem1.Click
        FrmSecurity.Close()
        FrmSecurity.Show()
    End Sub

    Private Sub AddNewUserToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewUserToolStripMenuItem1.Click
        FrmAddUser.Close()
        FrmAddUser.Show()
    End Sub



    Private Sub EditPresentationToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditPresentationToolStripMenuItem.Click

        'FrmDiagnosis.MdiParent = Me
        FrmPresentDataEntry.Show()
        FrmPresentDataEntry.Focus()
    End Sub

    '    Private Sub PictureBox2_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox2.Click
    '       FrmPresentDataEntry.Show()
    '      FrmPresentDataEntry.Focus()
    ' End Sub

    'Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click
    '   FrmPresentation.Show()
    '  FrmPresentation.Focus()
    '
    'End Sub

    ' Private Sub PictureBox3_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox3.Click
    '    btnLog.BackColor = Color.Red
    '   btnLog.ImageIndex = 1
    '  btnLog.Text = "Out"
    ' btnLog.Refresh()
    'InsertAudit_LogOut()
    ' FrmDiagnosis.Close()
    'FrmBiodata.Close()
    'FrmLogout.ShowDialog()
    'FrmMain_Load(sender, e)
    'End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        FrmPresentDataEntry.Show()
        FrmPresentDataEntry.Focus()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        FrmPresentation.Show()
        FrmPresentation.Focus()
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        If GV_CUR_PATID = "" Then
            MsgBox("Please select patient")
            FrmSearchPat.Show()
            FrmSearchPat.Focus()
            Exit Sub
        End If
        'FrmDiagnosis.MdiParent = Me
        FrmDiagnosis.Show()
        FrmDiagnosis.Focus()
    End Sub

    Private Sub ImportToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ImportButton.Click
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("updatepatientsinfo", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        Try
            myCommand.ExecuteNonQuery()
            MsgBox("Patients have been successfully imported into this week BTB presentation.")

        Catch ex As Exception
            MsgBox("Import Patients into BTB failed, pls contact administrator John @63266295")
        End Try

    End Sub
End Class